//
//  B_IntegralExpendVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/27.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_IntegralExpendVC.h"
#import "B_IntegralExpendTVC.h"
#import "B_IntegralExpendModel.h"
#import "B_IntegralOrderDetailVC.h"


@interface B_IntegralExpendVC ()
@property (nonatomic, strong) BaseTableView * mainView;

@end

@implementation B_IntegralExpendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view = self.mainView;
    
    self.dataArray = [NSMutableArray array];
    [self getData:YES];
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_IntegralExpendTVC" bundle:nil] forCellReuseIdentifier:@"B_IntegralExpendTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 1;
        };
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            
            B_IntegralExpendModel * model = weSelf.dataArray[indexPath.row];
            B_IntegralExpendTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_IntegralExpendTVC" forIndexPath:indexPath];
            cell.B_titleLabel.text = model.goodsName;//@"跟我走,我是保证";
            cell.B_integralNumber.text = [NSString stringWithFormat:@"-%@积分", safeValue1(model.integralCount)];
            cell.B_timeLabel.text = [NSString stringWithFormat:@"兑换时间: %@", [NSObject getTime:model.createTime]];
            if ([model.type isEqualToString:@"1"]) {
                cell.B_statusLabel.text = @"待发货";
            } else if  ([model.type isEqualToString:@"2"]) {
                cell.B_statusLabel.text = @"已发货";
            } else if  ([model.type isEqualToString:@"3"]) {
                cell.B_statusLabel.text = @"已收货";
            } else {
                cell.B_statusLabel.hidden = YES;
            }
            cell.B_imageView.backgroundColor = [UIColor whiteColor];
            return cell;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 120;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_IntegralExpendModel * model = weSelf.dataArray[indexPath.row];
            B_IntegralOrderDetailVC * vc = [[B_IntegralOrderDetailVC alloc] init];
            vc.B_orderId = model.id;
            [weSelf pushNextViewController:vc];
        };
        
        [_mainView setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        [_mainView setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }];
        
    }
    return _mainView;
}

- (void) getData:(BOOL) isrefresh {
    if (isrefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userIntegral/userIntegralDetailExpend/%@/%ld/10", getUserId(), self.index] parameters:nil isCache:isrefresh succeed:^(id data) {
        [weSelf.mainView endRefresh];
        if (isrefresh) {
            [weSelf.dataArray removeAllObjects];
        }
        if ([data[@"code"] isEqualToString:@"20010"]) {
            for (NSDictionary * dictionary in data[@"values"]) {
                B_IntegralExpendModel * model = [B_IntegralExpendModel mj_objectWithKeyValues:dictionary];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.mainView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
